package fashionshop.bll;


import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;

import javax.swing.JTable;
import javax.swing.table.TableModel;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class StatisticBLL {
	public static void toExcel(JTable table, File file){
//	    BigDecimal sum = BigDecimal.ZERO;
//		try{
//	        TableModel model = table.getModel();
//	        FileWriter excel = new FileWriter(file);
//
//	        for(int i = 0; i < model.getColumnCount(); i++){
//	            excel.write(model.getColumnName(i) + "\t");
//	            System.out.println(model.getColumnName(i));
//	        }
//
//	        excel.write("\n");
//
//	        for(int i=0; i< model.getRowCount(); i++) {
//	            for(int j=0; j < model.getColumnCount(); j++) {
//	                excel.write(model.getValueAt(i,j).toString()+"\t");
////	                if(j==7)
////	                	sum = sum.add(new BigDecimal(model.getValueAt(i,j).toString()));
//	            }
//	            excel.write("\n");
//	        }
//	        
////	        excel.write("Tong cong:\t " + sum);
//
//	        excel.close();
//
//	    }catch(IOException e){ System.out.println(e); }
		
		
		try {

			WritableWorkbook workbook1 = Workbook.createWorkbook(file);
			WritableSheet sheet1 = workbook1.createSheet("First Sheet", 0);
			TableModel model = table.getModel();

			for (int i = 0; i < model.getColumnCount(); i++) {
				Label column = new Label(i, 0, model.getColumnName(i));
				sheet1.addCell(column);
			}
			int j = 0;
			for (int i = 0; i < model.getRowCount(); i++) {
				for (j = 0; j < model.getColumnCount(); j++) {
					Label row = new Label(j, i + 1, model.getValueAt(i, j)
							.toString());
					sheet1.addCell(row);
				}
			}
			workbook1.write();
			workbook1.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
